const CONFIG = require('../../../config.js');

let TEXTData = {};

Page({
	data: {
		navBgColor: '-webkit-gradient(linear, 0% 0%, 0% 100%,from(#1b64e5), to(#39a9ed))',
		loginInfo:null,
		isList: [ {name: '是', id: '1'}, {name: '否', id: '0'} ],
		workList: [ {name: '工薪族', id: '1'}, {name: '企业主', id: '2'}, {name: '自由职业', id: '3'}],
		marryList: [ {name: '未婚', id: '1'}, {name: '已婚', id: '2'}, {name: '离异', id: '3'}, {name: '丧偶', id: '4'}],
		hasHourseOrCar: [ {name: '有房', id: '0'}, {name: '有车', id: '1'} ],
		formData:{
			A001: '',
			A002: '',
			A003: '',
			A004: '',
			A009: '',
			A010: '',
			A015: '',
			A016: '',
			A017: '',
			A018: '',
			A019: '',
			A020: '',
			A021: '',
		},
		productTypeid: '',
		proId: '',
		TEXTDataList: [],
	},

	// 时间选择
	bindDateChange: function (e) {
		let {field} = e.currentTarget.dataset;
		this.setData({
			applyTime: e.detail.value,
			[`formData.${field}`]: e.detail.value
		})
	},
  	// 单选
	  radioChange: function (e) {
		const {field} = e.currentTarget.dataset;
		this.setData({
			[`formData.${field}`]: e.currentTarget.dataset.value
		})
	},
	// 多选
	checkChange: function (e) {
		const {field} = e.currentTarget.dataset;
		let val = e.currentTarget.dataset.value;
		// 以上皆无 
		if((field === 'creditHaveArr' && val == '4') || (field === 'creditCardHaveArr' && val == '5')){
			this.setData({
				[`formData.${field}`]: []
			})
		}else{
			let idx = this.data.formData[field].indexOf(val);
			if(idx > -1){
				this.data.formData[field].splice(idx, 1);
			}else{
				this.data.formData[field].push(val);
			}
			this.setData({
				[`formData.${field}`]: this.data.formData[field]
			})
		}
	},
	// 输入框
	formInputChange(e) {
		const {field} = e.currentTarget.dataset;
		let val = e.detail.value;
		if(field === 'A001' || field === 'A002') {
			if(val > 100){
				val = val.substring(0,2);
				wx.showToast({ title: '年龄不能超过100岁', icon: 'none', duration: 2000 });
			}else{
				val = Number(val);
			}
		}
		this.setData({
			[`formData.${field}`]: val
		})
	},

	// 数据回显
	getDataFn: function(){
		let that = this;
		wx.request({
			url: CONFIG.host + "/system/appInfoController/getCustomerProductDetailInfo",
			data: {
				itemType: 'A',
			},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 'success'){
					let data = res.data.data;
					for(let i = 0; i < data.length; i++){
						that.data.formData[data[i].itemCode] = data[i].itemValue;
					}
					this.setData({
						formData: that.data.formData
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	// 提交保存
	submitFn: function(){
		let datas = JSON.parse(JSON.stringify(this.data.formData));
		// 提交验证
		let isBool = datas.A001 === '' || datas.A009 === '';
		if(isBool){
			wx.showToast({
				title: '请填写完整！',
				icon: 'none',
			})
			return;
		}
		let productInfos = [];
		for(let key in datas){
			if(Array.isArray(datas[key])){
				datas[key] = datas[key].join(',');
			}
			let obj = {
				"itemCode": key,
				"itemValue": datas[key],
				"provinceCode": "",
				"cityCode": "",
				"areaCode": ""
			}
			productInfos.push(obj);
		}
		wx.request({
			url: CONFIG.host + "/system/appInfoController/addCustomerProductInfo",
			data: {
				"productItem":"A",
				"productInfos": productInfos
			},
			method: "POST",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				wx.showToast({
					title: res.data.message,
					icon: 'none',
				})
				if(res.data.code === 'success'){
					wx.redirectTo({
						url: '/pages/productList/productList',
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},


  	onLoad: function (options) {
			let showNum = wx.getStorageSync('showUser') === '0' ? '0' : '1';
			TEXTData = require('../../../utils/text'+ showNum +'.js');
			let loginInfo = wx.getStorageSync('loginInfo') || '';
			if(loginInfo){
				this.setData({
					loginInfo : JSON.parse(loginInfo),
					productTypeid: options.productTypeid,
					isTrueData: wx.getStorageSync('showUser'),
					TEXTDataList: TEXTData.textJson
				})
				this.getDataFn();
			}
	  },
	  
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		
	},
})
